package com.wangsanfei.ihg.wechat.controller;

import java.io.File;
import java.util.Date;

import com.jfinal.core.ActionKey;
import com.jfinal.kit.PathKit;
import com.jfinal.kit.PropKit;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.upload.UploadFile;
import com.wangsanfei.base.BaseController;
import com.wangsanfei.model.FileInfo;
import com.wangsanfei.model.User;
import com.wangsanfei.util.FileUtil;
import com.wangsanfei.vo.BaseVo;

public class FileController extends BaseController{

	@ActionKey("/manage/addCkeditorImage")
	public void addCkeditorImage(){
		FileInfo fileInfo=addFile("upload", null);

		String filepath=getRequest().getContextPath()+"/file?id="+fileInfo.getStr("id");
		StringBuffer str=new StringBuffer("");
		String callback=getPara("CKEditorFuncNum");
		str.append("<script type=\"text/javascript\">");
		str.append("window.parent.CKEDITOR.tools.callFunction("+ callback + ",'"+filepath+"','')");
		str.append("</script>");
		renderHtml(str.toString());
	}
	
	
	@ActionKey("/ckeditorImage")
	public void ckeditorImage(){
		String filename=getPara("filename");
		String path=PropKit.get("file.storePath");
		File f=new File(path+filename);
		renderFile(f);
	}
	
	
	@ActionKey("/file")
	public void file(){
		String id=getPara("id");
		FileInfo fileInfo=FileInfo.dao.findById(id);
		String path=PropKit.get("file.storePath");
		System.out.println(PathKit.getWebRootPath());
		String filepath=PathKit.getWebRootPath()+"/upload/"+fileInfo.getStr("fileName");
		renderFile(new File(filepath));
	}
	
	@ActionKey("/fileFj")
	public void fileFj(){
		String id=getPara("id");
		FileInfo fileInfo=FileInfo.dao.findById(id);
		
		if(fileInfo.getStr("contentType").contains("image")){
			setAttr("id", id);
			renderJsp("/img.jsp");
		}else{
			String path=PropKit.get("file.storePath");
			String filepath=path+fileInfo.getStr("fileName");
			renderFile(new File(filepath));
			
		}
	}
	
	@ActionKey("/fileInfo")
	public void fileInfo(){
		BaseVo baseVo=new BaseVo();
		
		String id=getPara("id");
		FileInfo fileInfo=FileInfo.dao.findById(id);
		
		if(fileInfo!=null){
			baseVo.setFlag(true);
			baseVo.setData(fileInfo);
		}else{
			baseVo.setFlag(false);
		}
		
		renderJson(baseVo);
	}
	
	@ActionKey("/file/upload")
	public void upload(){
		FileInfo fileInfo=addFile("file", null);
		BaseVo baseVo=new BaseVo();
		baseVo.setData(fileInfo.getStr("id"));
		baseVo.setFlag(true);
		renderJson(baseVo);
	}
	
	@ActionKey("/fileByFilename")
	public void fileByFilename(){
		
		String filename=getPara("filename");
		renderFile(new File(PropKit.get("file.storePath")+filename));
	}
	
	@ActionKey("/manage/banner")
	public void manage_banner(){
		
		Record record=new Record();
		
		record.set("searchContent", getPara("searchContent"));
		Page<Record> page=FileInfo.dao.pagequeryBanner(getParaToInt("pageNumber"), getParaToInt("pageSize"),record);
		
		setAttr("nav", "banner");
		setAttr("page", page);
		setAttr("record", record);
		
		renderJsp("/manage/banner/banner-list.jsp");
	}
	
	@ActionKey("/manage/banner/addDisp")
	public void manage_banner_addDisp(){
		
		setAttr("now", new Date());
		setAttr("nav", "banner");
		
		renderJsp("/manage/banner/banner-add.jsp");
	}
	
	@ActionKey("/manage/banner/add")
	public void manage_banner_add(){
		
		User currenUser=getCurrenUser();
		
		FileInfo banner=addFile("banner", currenUser.getStr("id"));
		
		String desp=getPara("desp");
		String url=getPara("url");
		
		banner.set("desp", desp);
		banner.set("url", url);
		banner.set("lx", "1");
		banner.update();
		
		setAttr("nav", "banner");
		redirect("/manage/banner");
	}
	
	@ActionKey("/manage/banner/editDisp")
	public void manage_banner_editDisp(){
		
		String id=getPara("id");
		
		FileInfo fileInfo=FileInfo.dao.findById(id);
		
		setAttr("vo", fileInfo);
		setAttr("nav", "banner");
		renderJsp("/manage/banner/banner-edit.jsp");
	}
	
	@ActionKey("/manage/banner/edit")
	public void manage_banner_edit(){
		
		User currenUser=getCurrenUser();
		
		String path=PropKit.get("file.storePath");
		UploadFile uploadFile=getFile("banner", path);
		
		String id=getPara("id");
		String desp=getPara("desp");
		String url=getPara("url");
		FileInfo fileInfo=FileInfo.dao.findById(id);
		
		
		if(uploadFile!=null){
			fileInfo.set("originalFileName", uploadFile.getOriginalFileName());
			fileInfo.set("fileName", uploadFile.getFileName());
			fileInfo.set("contentType", uploadFile.getContentType());
			fileInfo.set("md5", FileUtil.getMd5ByFile(uploadFile.getFile()));
		}
		
		fileInfo.set("desp", desp);
		fileInfo.set("url", url);
		
		fileInfo.update();
		
		
		setAttr("nav", "banner");
		redirect("/manage/banner");
	}
	
	@ActionKey("/manage/banner/del")
	public void manage_banner_del(){
		
		String id=getPara("id");
		FileInfo fileInfo=FileInfo.dao.findById(id);
		fileInfo.delete();
		setAttr("nav", "banner");
		redirect("/manage/banner");
	}

}
